package com.llkj.qingnianguoxue.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.GuidePageBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class GuidePageActivity extends BaseActivity implements
        OnPageChangeListener {
    private ViewPager vp_guide;
    private LinearLayout ll_group;
    private List<View> list = new ArrayList<View>();
    private List<ImageView> mPointViews = new ArrayList<ImageView>();
    private int imagesid[] = {R.drawable.ic_start1,
            R.drawable.ic_start2, R.drawable.ic_start3, R.drawable.ic_start4};// 图片资源ID
    private boolean isCanFinish = false;//判断到最后一页是否可以跳转

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guidepage);
//        systemBarTintManager.setStatusBarTintResource(R.color.transparent);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,null,R.color.transparent);
        isFirst();
//        init();
    }

    private void isFirst() {
        if (!MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISFIRSTOPEN)) {
            MyApplication.getInstance().sputil.put(ConstantUserInfo.ISFIRSTOPEN, true);
            init(imagesid);
//            HttpMethod.bootPage(this, this);
        } else {
            Intent intent = new Intent(this, WelcomeActivity.class);
            startActivity(intent);
            this.finish();
        }
    }

    private void init(int[] listData) {
        vp_guide = (ViewPager) findViewById(R.id.vp_guide);
        ll_group = (LinearLayout) findViewById(R.id.ll_group);
        list = new ArrayList<View>();
        ImageView iv = null;
        for (int i = 0; i < listData.length; i++) {
            iv = new ImageView(this);
            iv.setImageResource(imagesid[i]);
            ImageLoadingUtil.loadLocalImgCenterCrop(iv, imagesid[i]);
//            ImageLoadingUtil.loadingImg(iv, listData.get(i), 1);

            list.add(iv);
            iv = new ImageView(this);
            iv.setPadding(10, 0, 10, 0);
            if (mPointViews.isEmpty()) {
                iv.setImageResource(R.drawable.page_point_selected);
            } else {
                iv.setImageResource(R.drawable.page_point_normal);
            }
            mPointViews.add(iv);
            ll_group.addView(iv);
        }

        GuidePageAdapter adapter = new GuidePageAdapter();
        vp_guide.setAdapter(adapter);
        vp_guide.setOnPageChangeListener(this);
    }

    private class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(list.get(position));
            return list.get(position);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            if (vp_guide.getAdapter().getCount() - 1 == vp_guide
                    .getCurrentItem()) {
                if (!isCanFinish) {
                    isCanFinish = true;
                } else {
                    Intent intent = new Intent(this, WelcomeActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int position) {
        if (vp_guide.getAdapter().getCount() - 1 != vp_guide
                .getCurrentItem()) {
            isCanFinish = false;
        }
        mPointViews.get(position).setImageResource(
                R.drawable.page_point_selected);
        for (int i = 0; i < mPointViews.size(); i++) {
            if (i != position) {
                mPointViews.get(i).setImageResource(
                        R.drawable.page_point_normal);
            }
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_LOGIN_BOOT_PAGE) {
            try {
                GuidePageBean guidePageBean = GsonUtil.GsonToBean(data, GuidePageBean.class);
                List<String> list = guidePageBean.getList();
//                init(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
